package com.sphd.education.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.sphd.education.R;
import com.sphd.education.adapter.HistorySearchAdapter;
import com.sphd.education.info.KeyWordInfo;
import com.sphd.education.interfaces.SendSearchInterface;

import org.litepal.crud.DataSupport;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hu on 2016/3/2.
 */
public class HomeSearchFragment extends Fragment  {
    Bundle bundle;
    @Bind(R.id.history_search)
    ListView historySearch;
    @Bind(R.id.clear_btn)
    Button btnClear;
    private List<KeyWordInfo> keyWordInfoList;
    HistorySearchAdapter searchAdapter;
    SendSearchInterface sendCallBack;
    public static HomeSearchFragment newInstance(Bundle bundle) {
        HomeSearchFragment fragment = new HomeSearchFragment();
        fragment.setArguments(bundle);//传值判断是那个fragment
        return fragment;
    }
    public void setSendCallBack(SendSearchInterface sendCallBack){
        this.sendCallBack=sendCallBack;
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        bundle = new Bundle();
        View view = inflater.inflate(R.layout.home_search_fragment, container, false);
        ButterKnife.bind(this, view);
        initView();
        initEvent();
        return view;
    }



    private void initView() {
        keyWordInfoList = DataSupport.findAll(KeyWordInfo.class);
        searchAdapter = new HistorySearchAdapter(keyWordInfoList);
        historySearch.setAdapter(searchAdapter);
    }
    private void initEvent() {
        historySearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (keyWordInfoList.get(position).getType() == 2) {
                    sendCallBack.SendSearchCallBack(2, keyWordInfoList.get(position).getWord());
                } else if (keyWordInfoList.get(position).getType() == 1) {
                    sendCallBack.SendSearchCallBack(1, keyWordInfoList.get(position).getWord());
                }
            }
        });
        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataSupport.deleteAll(KeyWordInfo.class);
                keyWordInfoList.clear();
                searchAdapter.notifyDataSetChanged();
            }
        });
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
